# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
sys.path.append('../')
sys.path.append('../common')

from PyQt4 import QtCore
from PyQt4.QtGui import *

from ui_toolsWidget import Ui_ToolsDockWidget
from ui_itemsWidget import Ui_ItemsDockWidget

from tools import *

from minimize import GenCoords

import openbabel

class ToolWidget(QDockWidget):

	def __init__(self, scene, parent=None):
		QDockWidget.__init__(self, parent)

		self.ui = Ui_ToolsDockWidget()
		self.ui.setupUi(self)

		self.scene = scene
		self.scene.setTool(self.scene.draw)
		self.scene.setBond("single")

		self.toolButtonGroup = QButtonGroup()
		self.toolButtonGroup.addButton(self.ui.drawPushButton)
		self.toolButtonGroup.addButton(self.ui.movePushButton)
		self.toolButtonGroup.addButton(self.ui.rotatePushButton)
		self.toolButtonGroup.addButton(self.ui.erasePushButton)
		self.toolButtonGroup.addButton(self.ui.flipPushButton)

		self.bondButtonGroup = QButtonGroup()
		self.bondButtonGroup.addButton(self.ui.singlePushButton)
		self.bondButtonGroup.addButton(self.ui.doublePushButton)
		self.bondButtonGroup.addButton(self.ui.triplePushButton)
		self.bondButtonGroup.addButton(self.ui.wedgePushButton)
		self.bondButtonGroup.addButton(self.ui.hashPushButton)
		self.bondButtonGroup.addButton(self.ui.squigglePushButton)
		self.bondButtonGroup.addButton(self.ui.cistransPushButton)

		self.connect(self.ui.drawPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.draw))
		self.connect(self.ui.movePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.move))
		self.connect(self.ui.rotatePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.rotate))
		self.connect(self.ui.flipPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.flip))
		self.connect(self.ui.erasePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.erase))
		self.connect(self.ui.singlePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("single"))
		self.connect(self.ui.doublePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("double"))
		self.connect(self.ui.triplePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("triple"))
		self.connect(self.ui.wedgePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("wedge"))
		self.connect(self.ui.hashPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("hash"))
		self.connect(self.ui.squigglePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("squiggle"))
		self.connect(self.ui.cistransPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("cistrans"))
		self.connect(self.ui.minimizePushButton, QtCore.SIGNAL("clicked()"), self.scene.minimizeCompound)


class ItemWidget(QDockWidget):

	def __init__(self, scene, parent=None):
		QDockWidget.__init__(self, parent)

		self.ui = Ui_ItemsDockWidget()
		self.ui.setupUi(self)

		self.scene = scene

		self.buttonGroup = QButtonGroup()
		self.buttonGroup.addButton(self.ui.hydrogenPushButton)
		self.buttonGroup.addButton(self.ui.carbonPushButton)
		self.buttonGroup.addButton(self.ui.nitrogenPushButton)
		self.buttonGroup.addButton(self.ui.oxygenPushButton)
		self.buttonGroup.addButton(self.ui.phosphorusPushButton)
		self.buttonGroup.addButton(self.ui.sulfurPushButton)
		self.buttonGroup.addButton(self.ui.fluorinePushButton)
		self.buttonGroup.addButton(self.ui.chlorinePushButton)
		self.buttonGroup.addButton(self.ui.brominePushButton)
		self.buttonGroup.addButton(self.ui.iodinePushButton)
		self.buttonGroup.addButton(self.ui.ring3PushButton)
		self.buttonGroup.addButton(self.ui.ring4PushButton)
		self.buttonGroup.addButton(self.ui.ring5PushButton)
		self.buttonGroup.addButton(self.ui.ring6PushButton)
		self.buttonGroup.addButton(self.ui.ring7PushButton)
		self.buttonGroup.addButton(self.ui.ring8PushButton)
		self.buttonGroup.addButton(self.ui.ring5DoublePushButton)
		self.buttonGroup.addButton(self.ui.ring6DoublePushButton)

		self.makeItems()

		self.scene.setAtom(self.carbon)

		self.connect(self.ui.hydrogenPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.hydrogen))
		self.connect(self.ui.carbonPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.carbon))
		self.connect(self.ui.nitrogenPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.nitrogen))
		self.connect(self.ui.oxygenPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.oxygen))
		self.connect(self.ui.phosphorusPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.phosphorus))
		self.connect(self.ui.sulfurPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.sulfur))
		self.connect(self.ui.fluorinePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.fluorine))
		self.connect(self.ui.chlorinePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.chlorine))
		self.connect(self.ui.brominePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.bromine))
		self.connect(self.ui.iodinePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setAtom(self.iodine))

		self.connect(self.ui.ring3PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(3))
		self.connect(self.ui.ring4PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(4))
		self.connect(self.ui.ring5PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(5))
		self.connect(self.ui.ring6PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(6))
		self.connect(self.ui.ring7PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(7))
		self.connect(self.ui.ring8PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(8))
		self.connect(self.ui.ring5DoublePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(5, True))
		self.connect(self.ui.ring6DoublePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setRing(6, True))

	def makeItems(self):
		self.hydrogen = openbabel.OBAtom()
		self.hydrogen.SetAtomicNum(1)
		self.carbon = openbabel.OBAtom()
		self.carbon.SetAtomicNum(6)
		self.nitrogen = openbabel.OBAtom()
		self.nitrogen.SetAtomicNum(7)
		self.oxygen = openbabel.OBAtom()
		self.oxygen.SetAtomicNum(8)
		self.phosphorus = openbabel.OBAtom()
		self.phosphorus.SetAtomicNum(15)
		self.sulfur = openbabel.OBAtom()
		self.sulfur.SetAtomicNum(16)
		self.fluorine = openbabel.OBAtom()
		self.fluorine.SetAtomicNum(9)
		self.chlorine = openbabel.OBAtom()
		self.chlorine.SetAtomicNum(17)
		self.bromine = openbabel.OBAtom()
		self.bromine.SetAtomicNum(35)
		self.iodine = openbabel.OBAtom()
		self.iodine.SetAtomicNum(53)

		gen = GenCoords()

		self.ring3 = openbabel.OBMol()
		ra1=self.ring3.NewAtom()
		ra2=self.ring3.NewAtom()
		ra3=self.ring3.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		self.ring3.AddBond(1,2,1)
		self.ring3.AddBond(2,3,1)
		self.ring3.AddBond(3,1,1)

		self.ring4 = openbabel.OBMol()
		ra1=self.ring4.NewAtom()
		ra2=self.ring4.NewAtom()
		ra3=self.ring4.NewAtom()
		ra4=self.ring4.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		self.ring4.AddBond(1,2,1)
		self.ring4.AddBond(2,3,1)
		self.ring4.AddBond(3,4,1)
		self.ring4.AddBond(4,1,1)

		self.ring5 = openbabel.OBMol()
		ra1=self.ring5.NewAtom()
		ra2=self.ring5.NewAtom()
		ra3=self.ring5.NewAtom()
		ra4=self.ring5.NewAtom()
		ra5=self.ring5.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		ra5.SetAtomicNum(6)
		self.ring5.AddBond(1,2,1)
		self.ring5.AddBond(2,3,1)
		self.ring5.AddBond(3,4,1)
		self.ring5.AddBond(4,5,1)
		self.ring5.AddBond(5,1,1)

		self.ring6 = openbabel.OBMol()
		ra1=self.ring6.NewAtom()
		ra2=self.ring6.NewAtom()
		ra3=self.ring6.NewAtom()
		ra4=self.ring6.NewAtom()
		ra5=self.ring6.NewAtom()
		ra6=self.ring6.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		ra5.SetAtomicNum(6)
		ra6.SetAtomicNum(6)
		self.ring6.AddBond(1,2,1)
		self.ring6.AddBond(2,3,1)
		self.ring6.AddBond(3,4,1)
		self.ring6.AddBond(4,5,1)
		self.ring6.AddBond(5,6,1)
		self.ring6.AddBond(6,1,1)
		
		self.ring7 = openbabel.OBMol()
		ra1=self.ring7.NewAtom()
		ra2=self.ring7.NewAtom()
		ra3=self.ring7.NewAtom()
		ra4=self.ring7.NewAtom()
		ra5=self.ring7.NewAtom()
		ra6=self.ring7.NewAtom()
		ra7=self.ring7.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		ra5.SetAtomicNum(6)
		ra6.SetAtomicNum(6)
		ra7.SetAtomicNum(6)
		self.ring7.AddBond(1,2,1)
		self.ring7.AddBond(2,3,1)
		self.ring7.AddBond(3,4,1)
		self.ring7.AddBond(4,5,1)
		self.ring7.AddBond(5,6,1)
		self.ring7.AddBond(6,7,1)
		self.ring7.AddBond(7,1,1)
		
		self.ring8 = openbabel.OBMol()
		ra1=self.ring8.NewAtom()
		ra2=self.ring8.NewAtom()
		ra3=self.ring8.NewAtom()
		ra4=self.ring8.NewAtom()
		ra5=self.ring8.NewAtom()
		ra6=self.ring8.NewAtom()
		ra7=self.ring8.NewAtom()
		ra8=self.ring8.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		ra5.SetAtomicNum(6)
		ra6.SetAtomicNum(6)
		ra7.SetAtomicNum(6)
		ra8.SetAtomicNum(6)
		self.ring8.AddBond(1,2,1)
		self.ring8.AddBond(2,3,1)
		self.ring8.AddBond(3,4,1)
		self.ring8.AddBond(4,5,1)
		self.ring8.AddBond(5,6,1)
		self.ring8.AddBond(6,7,1)
		self.ring8.AddBond(7,8,1)
		self.ring8.AddBond(8,1,1)
		
		self.ring5Double = openbabel.OBMol()
		ra1=self.ring5Double.NewAtom()
		ra2=self.ring5Double.NewAtom()
		ra3=self.ring5Double.NewAtom()
		ra4=self.ring5Double.NewAtom()
		ra5=self.ring5Double.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		ra5.SetAtomicNum(6)
		self.ring5Double.AddBond(1,2,1)
		self.ring5Double.AddBond(2,3,2)
		self.ring5Double.AddBond(3,4,1)
		self.ring5Double.AddBond(4,5,2)
		self.ring5Double.AddBond(5,1,1)
		
		self.ring6Double = openbabel.OBMol()
		ra1=self.ring6Double.NewAtom()
		ra2=self.ring6Double.NewAtom()
		ra3=self.ring6Double.NewAtom()
		ra4=self.ring6Double.NewAtom()
		ra5=self.ring6Double.NewAtom()
		ra6=self.ring6Double.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		ra5.SetAtomicNum(6)
		ra6.SetAtomicNum(6)
		self.ring6Double.AddBond(1,2,1)
		self.ring6Double.AddBond(2,3,2)
		self.ring6Double.AddBond(3,4,1)
		self.ring6Double.AddBond(4,5,2)
		self.ring6Double.AddBond(5,6,1)
		self.ring6Double.AddBond(6,1,2)
